#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[200005],ans;
void dfs(int r, int b, int sum, int dep){
    if (dep == n){
        ans = max(ans, sum);
        return;
    }
    dep++;
    dfs(a[dep], b, sum+(r==a[dep]?r:0), dep);
    dfs(r, a[dep], sum+(b==a[dep]?b:0), dep);
}
int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int T;
    scanf("%d", &T);
    while (T --){
        ans = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++){
            scanf("%d", &a[i]);
        }
        dfs(0, 0, 0, 0);
        printf("%d\n", ans);
    }
    return 0;
}